# Copyright (C) 2007 Laszlo Pandy <laszlok2@gmail.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

from AdventureMoney.core import Person
from AdventureMoney.core import ShareProfile
from AdventureMoney.core import Category
from AdventureMoney.core import Bill

class Loader:
	def __init__(self, doc, object):
		self.LoadAccountFromXML(doc, object)
		
	def LoadAccountFromXML(self, node, account):
		peopleNode = node.getElementsByTagName("People")[0]
		personNodes = peopleNode.getElementsByTagName("Person")
		for person in personNodes:
			self.LoadPersonFromXML(person, account)
		
		categoriesNode = node.getElementsByTagName("Categories")[0]
		categoryNodes = categoriesNode.getElementsByTagName("Category")
		for category in categoryNodes:
			account.categories.add(self.LoadCategoryFromXML(category, account))
		
		billsNode = node.getElementsByTagName("Bills")[0]
		billNodes = billsNode.getElementsByTagName("Bill")
		for bill in billNodes:
			tempBill = self.LoadBillFromXML(bill, account)
			account._addBill(tempBill)
		
	def LoadPersonFromXML(self, node, account):
		name = str(node.getAttribute("name"))
		person = Person.Person(name)
		account.addPerson(person)
		if node.hasAttribute("manager"):
			account.setManager(person)
			
		return person
		
	
	def LoadCategoryFromXML(self, node, account):
		categoryName = str(node.getAttribute("name"))
		
		paidBy = str(node.getAttribute("paidBy"))
		if paidBy:
			paidPerson = account.findPerson(paidBy)
		else:
			paidPerson = None
		
		shared = str(node.getAttribute("sharedBy"))
		shared = shared.split("|")
		sharedProfile = ShareProfile.ShareProfile()
		for name in shared:
			p = account.findPerson(name)
			sharedProfile.setPersonShare(p, ShareProfile.SHARE_EQUAL)
		
		return Category.Category(categoryName, paidPerson, sharedProfile)
		
		
	def LoadBillFromXML(self, node, account):
		value = str(node.getAttribute("value"))
		date = str(node.getAttribute("date"))
		paidBy = str(node.getAttribute("paidBy"))
		details = str(node.getAttribute("details"))
		comments = str(node.getAttribute("comments"))
		
		if node.hasAttribute("category"):
			sharedBy = account.findCategory(str(node.getAttribute("category")))
		else:
			sharedStr = str(node.getAttribute("sharedBy"))
			sharedStr = sharedStr.split("|")
			sharedBy = ShareProfile.ShareProfile()
			for name in sharedStr:
				p = account.findPerson(name)
				sharedBy.setPersonShare(p, ShareProfile.SHARE_EQUAL)
		
		# small bug in version 1 would put "None" in paidBy when categories were used
		if paidBy and paidBy != "None":
			paidPerson = account.findPerson(paidBy)
		else:
			paidPerson = None
		
		bill = Bill.Bill(value, date, paidPerson, sharedBy, details, comments)
		return bill
